﻿using System;
using System.Collections.Generic;
using System.Data.SqlTypes;
using System.Diagnostics;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace OrdersManage
{
    [Serializable]
    public class Order:IComparable
    {
        public int OdNum{ get;set;}
        public double money
        { get {  double allmoney = 0;
                foreach (OrderDetails detail in details)
                {
                    allmoney += detail.GetThisPrice();
                }
                return allmoney;
            } 
        }
        public string Client { get; set; }
        public string date { get; set; }
        public List<OrderDetails> details = new List<OrderDetails>();
        public Order() 
        {
            this.OdNum = 0;
            this.Client = string.Empty;
            this.date = string.Empty;
        }
        public Order(int odnum, string client, string date, List<OrderDetails> details)
        {
            OdNum = odnum;
            Client = client;
            this.date = date;
            this.details = details;
        }
        public int CompareTo(object obj)
        {
            if (!(obj is Order)) throw new ArgumentException();
            Order order = (Order)obj;
            return this.OdNum.CompareTo(order.OdNum);
        }
        public override string ToString() 
        {
            return "订单号：" + OdNum + " 客户：" + Client + " 金额：" + money + " 日期：" + date;
        }

        public override bool Equals(object obj)
        {
            return obj is Order order &&
                   OdNum == order.OdNum;
        }

        public override int GetHashCode()
        {
            return Convert.ToInt32(OdNum);
        }
        public void addDetail(OrderDetails detail)
        {
            details.Add(detail);
        }
        public void addDetail(string name, int cnt, double singlePrice)
        {
            try
            {
                OrderDetails detail = new OrderDetails(name, cnt, singlePrice);
                foreach (OrderDetails od in details)
                {
                    if (od.Equals(detail))
                    { Exception e = new Exception();
                        throw e;
                    }
                }
                details.Add(detail);
            }
            catch //(Exception e)
            {
                Console.WriteLine("该订单明细已存在！");
            }
        }
        public void removeDetail(OrderDetails detail)
        {
            try { details.Remove(detail); }
            catch { Console.WriteLine("该订单中不包含此项明细！"); }
        }
        public void showDetails()
        {
            Console.WriteLine("序号 名称 数量 单价");
            foreach (OrderDetails a in details)
            {

                Console.WriteLine("-----------------------");
                Console.WriteLine("{0} {1} {2} {3}", details.IndexOf(a), a.Name, a.Cnt, a.SinglePrice);
            }
       
        }
    }
}
